﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using FlashCards.Entities;

namespace FlashCards.Service
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
    public class FlashCardsService : IFlashCardsService
    {
        FlashCardEntities context;

        public FlashCardsService()
        {
            context = new FlashCardEntities();
        }

        #region IFlashCardsService Members

        public User Login(string loginName, string password)
        {
            throw new NotImplementedException();
        }

        public List<Language> ListLanguages()
        {
            var languages = context.Languages;

            return languages.ToList();
        }

        public List<Unit> ListUnits()
        {            
            throw new NotImplementedException();
        }

        public List<Word> ListWords(string wordFilter, string categoryFilter, Language language)
        {
            var words = from w in context.Words
                        where w.Language.Id == language.Id
                        && (wordFilter == null || w.OriginalWord.Contains(wordFilter))
                        && (categoryFilter == null || w.GrammaticalCategory.Contains(categoryFilter))
                        select w;

            return words.ToList();
        }

        public Word GetWord(long id)
        {
            var query = from w in context.Words.Include("Language").Include("ModifiedBy").Include("RecordedBy").Include("Unit")
                        where w.Id == id
                        select w;

            return query.FirstOrDefault();
                        
        }

        public long StoreWord(Word word)
        {
            if (word.Id == 0)
            {
                Word eWord = new Word();
                eWord.OriginalWord = word.OriginalWord;
                eWord.Pronunciation = word.Pronunciation;
                eWord.Definition = word.Definition;
                eWord.SampleSentence = word.SampleSentence;
                eWord.ModifiedAt = eWord.RecordedAt = DateTime.Now;


                context.AddToWords(eWord);
                context.SaveChanges();

                return eWord.Id;
            }
            else
            {
                Word eWord = (from w in context.Words
                             where w.Id == word.Id
                             select w).FirstOrDefault();

                eWord.OriginalWord = word.OriginalWord;
                eWord.Pronunciation = word.Pronunciation;
                eWord.Definition = word.Definition;
                eWord.SampleSentence = word.SampleSentence;
                eWord.ModifiedAt = DateTime.Now;

                context.SaveChanges();

                return eWord.Id;
            }
        }

        public string X()
        {
            return "XXX";
        }

        #endregion
    }
}
